// sdl_palyer.cpp
#include "display.h"
#include <iostream>
 
SDL2Player::SDL2Player() 
{
    Init();
}
 
SDL2Player::~SDL2Player() 
{
    Destroy();
}
 
void SDL2Player::Init() 
{
    if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) == -1) {
        std::cout << "SDL2 init failed" << std::endl;
        return;
    }
}
 
void SDL2Player::Destroy() 
{
    if(texture_) {
        std::cout << "SDL_DestroyTexture" << std::endl;
        SDL_DestroyTexture(texture_);
    }
    if (renderer_) {
        std::cout << "SDL_DestroyRenderer" << std::endl;
        SDL_DestroyRenderer(renderer_);
    }
    if (window_) {
        std::cout << "SDL_DestroyWindow" << std::endl;
        SDL_DestroyWindow(window_);
    }
    SDL_Quit();
    isInit_ =false;
}
 
void SDL2Player::Init(uint32_t width, uint32_t height, SDL2Player::PixFormat pix_format) 
{
    width_ = width;
    height_ = height;
    pix_format_ = pix_format;
}
 
void SDL2Player::CreateRenderWindow(enum SDLWindowsFlag flag, int x, int y) 
{
    window_ = SDL_CreateWindow(" ",
        x,
        y,
        width_, height_,
        SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE | flag 
    );
    InitRender();
    InitTexture();
    isInit_ = true;
}

 
void SDL2Player::SetWindow(void* handle) 
{
    // SDL_SetHint(SDL_HINT_VIDEO_WINDOW_SHARE_PIXEL_FORMAT, nullptr);
    window_ = SDL_CreateWindowFrom(handle);
    if (window_ == nullptr) {
        std::cout << SDL_GetError() << std::endl;
        std::cout << "window is nullptr" << std::endl;
    }
    InitRender();
    InitTexture();

    isInit_ = true;
}
bool SDL2Player::isInit()
{
    return isInit_;
}
void SDL2Player::GetCurrentScreenSize(uint32_t& width, uint32_t& height)
{
    // int display_count = SDL_GetNumVideoDisplays();
    // printf("Number of displays: %d\n", display_count);
    // for (int i = 0; i < display_count; ++i) {
    SDL_DisplayMode current_display_mode_;
    SDL_GetCurrentDisplayMode(0, &current_display_mode_);
    width=current_display_mode_.w;
    height=current_display_mode_.h;
    // }
    
}
void SDL2Player::InitRender() 
{
    
    renderer_ = SDL_CreateRenderer(window_, -1, SDL_RENDERER_ACCELERATED);
    SDL_RenderClear(renderer_);
}
 
void SDL2Player::InitTexture() 
{
    uint32_t pixformat;
    if (pix_format_ == I420) {
        pixformat = SDL_PIXELFORMAT_IYUV;
    }
    else if(pix_format_ == YUYV){
        pixformat = SDL_PIXELFORMAT_YV12;
    }
    else if (pix_format_ == NV12) {
        pixformat = SDL_PIXELFORMAT_NV12;
    }
    else if (pix_format_ == NV21) {
        pixformat = SDL_PIXELFORMAT_NV21;
    }
    else if(pix_format_ == RGB){
        pixformat = SDL_PIXELFORMAT_RGB24;
    }
    else {
        std::cout << "输入的格式暂不支持" << std::endl;
    }
    texture_ = SDL_CreateTexture(renderer_, pixformat, SDL_TEXTUREACCESS_STREAMING, width_, height_);
 
}
enum SDL2Player::SDLEvent SDL2Player::GetEvent() 
{
    SDL_Event event;
    if(SDL_PollEvent(&event)) {/*事件监测*/
        if(event.type==SDL_QUIT){/*退出事件*/
            return QUIT;
        }
        if(event.type==SDL_APP_TERMINATING){/*退出事件*/
            return TERMINATING;
        }
    }
    return NOTHING;
}

void SDL2Player::Render(char* data) 
{
    int pitch = width_;
    if(pix_format_ == RGB)
            pitch = width_*3;
    SDL_UpdateTexture(texture_, NULL, data, pitch);
    SDL_Rect rect;
    rect.x = 0;
    rect.y = 0;
    rect.w = width_;
    rect.h = height_;
    
    SDL_RenderClear(renderer_);
    SDL_RenderCopy(renderer_, texture_, NULL, &rect);
    SDL_RenderPresent(renderer_);
}